package com.ciban.smarthome.module.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ciban.smarthome.module.db.DbBaseDao;
import com.ciban.smarthome.module.entity.Equipment;
import com.ciban.smarthome.module.utils.ModuleUtils;

import java.util.ArrayList;

/**
 * Created by wdl on 2015/11/5.
 * 对于设备的数据管理类
 */
public class EquipmentDao extends DbBaseDao {
    public EquipmentDao(Context context) {
        super(context, "equipment");
    }
    /*_id integer PRIMARY KEY AUTOINCREMENT, type integer, serial_num integer, name varchar(20), icon varchar(30), area_id integer*/

    /**
     * 添加设备
     *
     * @param info
     * @return
     */
    public long addEquipMent(Equipment info) {
        ContentValues values = new ContentValues();
        values.put("_id", ModuleUtils.getIDByRule());
        values.put("type", info.getType());
        values.put("name", info.getName());
        values.put("serial_num", info.getSerial_no());
        values.put("icon", info.getIcon_path());
        values.put("area_id", info.getArea_id());

        return insert(null, values);
    }

    /**
     * 添加设备,手动设置id
     *
     * @param info
     * @return
     */
    public long addEquipMentByID(Equipment info) {
        ContentValues values = new ContentValues();
        values.put("type", info.getType());
        values.put("name", info.getName());
        values.put("serial_num", info.getSerial_no());
        values.put("icon", info.getIcon_path());
        values.put("area_id", info.getArea_id());
        values.put("_id",info.getId());

        return insert(null, values);
    }

    /**
     * 根据设备id将设备删除
     *
     * @param id
     * @return
     */
    public int delEquipMent(String id) {
        return del("_id=?", new String[]{id});
    }

    /**
     * 根据设备id 获取设备的详细信息
     *
     * @param id
     * @return
     */
    public Equipment getEquipMentDetail(String id) {
        Equipment info = null;
        Cursor cursor = query(new String[]{"_id", "serial_num", "type", "name", "icon", "area_id"}, "_id=?", new String[]{id}, null);
        if (cursor != null && cursor.moveToNext()) {
            info = new Equipment();
            info.setId(cursor.getString(0));
            info.setSerial_no(cursor.getInt(1));
            info.setType(cursor.getInt(2));
            info.setName(cursor.getString(3));
            info.setIcon_path(cursor.getString(4));
            info.setArea_id(cursor.getString(5));
        }
        return info;
    }

    /**
     * 根据区域名称获取该区域的所有设备
     *
     * @param area_id
     * @return
     */
    public ArrayList<Equipment> getEquipListByArea(String area_id) {
        return queryList("area_id=?", new String[]{area_id});
    }

    /**
     * 获取某个区域的某个类型的设备
     *
     * @param area_id
     * @param type
     * @return
     */
    public ArrayList<Equipment> getEquipListByAreaType(String area_id, int type) {
        return queryList("area_id=? and type=?", new String[]{area_id, type + ""});
    }

    /**
     * 获取某个类型的所有设备
     *
     * @param type
     * @return
     */
    public ArrayList<Equipment> getEquipListByType(int type) {
        return queryList("type=?", new String[]{type + ""});
    }

    public int updateEquip(Equipment info) {
        ContentValues values = new ContentValues();
        values.put("type", info.getType());
        values.put("name", info.getName());
        values.put("serial_num", info.getSerial_no());
        values.put("icon", info.getIcon_path());
        values.put("area_id", info.getArea_id());

        return update(values, "_id=?", new String[]{info.getId()});
    }

    /**
     * 获取设备列表
     *
     * @param where
     * @param args
     * @return
     */
    private ArrayList<Equipment> queryList(String where, String[] args) {
        ArrayList<Equipment> list = new ArrayList<Equipment>();
        Cursor cursor = query(new String[]{"_id", "serial_num", "type", "name", "icon", "area_id"}, where, args, null);
        Equipment info = null;
        while (cursor.moveToNext()) {
            info = new Equipment();
            info.setId(cursor.getString(0));
            info.setSerial_no(cursor.getInt(1));
            info.setType(cursor.getInt(2));
            info.setName(cursor.getString(3));
            info.setIcon_path(cursor.getString(4));
            info.setArea_id(cursor.getString(5));

            list.add(info);
        }
        return list;
    }
}
